package com.dfbz.demo01_实现TCP;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo02_Server {
    public static void main(String[] args) throws Exception{

        // 创建了一个服务器
        ServerSocket serverSocket = new ServerSocket(6666);

        // 接收一个客户端
        Socket client = serverSocket.accept();

        // 获取与客户端的输入流(用于读取客户端的数据)
        InputStream is = client.getInputStream();

        // 获取与客户端的输出流(用于向客户端写出数据)
        OutputStream os = client.getOutputStream();

        byte[] data=new byte[1024];
        int len = is.read(data);
        System.out.println("接收到来自客户端的信息【"+new String(data,0,len)+"】");

        os.write("在哦！".getBytes());

        len = is.read(data);
        System.out.println("接收到来自客户端的信息【"+new String(data,0,len)+"】");

        os.write("在吃饭呢！".getBytes());

        client.close();
        serverSocket.close();

    }
}
